﻿Imports MeoTom.Core.BLL
Imports System.IO
Imports MeoTom.Core

Partial Public Class PageCreate
    Inherits UI.Page
    Public A As New BLL.Pages()
    Public util2 As New Utilities.Common()
    Private PageID As String = ""

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim previousPage As String = ""
        If Request.QueryString.ToString = vbNullString Then
            previousPage = Server.UrlEncode(Request.ServerVariables("PATH_INFO"))
        Else
            previousPage = Server.UrlEncode(Request.ServerVariables("PATH_INFO") & "?" & Request.QueryString.ToString())
        End If
        util2.ValidateLogin(previousPage)

        ArticlePublishContent.InnerHtml = WriteScheduleDate()
        ArticleVisibleContent.InnerHtml = WriteVisibilityInf()
    End Sub

    Public Function WriteScheduleDate() As String
        Dim strOut As New StringBuilder()
        strOut.Append("<br /><select name=""mmmm"" id=""mmmm""><option value=""1"">Jan</option><option value=""2"">Feb</option><option value=""3"">Mar</option><option value=""4"">Apr</option><option value=""5"">May</option><option value=""6"">Jun</option><option value=""7"">Jul</option><option value=""8"">Aug</option><option value=""9"">Sep</option><option value=""10"">Oct</option><option value=""11"">Nov</option><option value=""12"">Dec</option></select> ")
        strOut.Append("<input type=""text"" style=""width:auto;"" id=""dd"" name=""dd"" value=""").Append(Day(DateTime.Now)).Append(""" size=""2"" maxlength=""2"" autocomplete=""off"" />")
        strOut.Append(", <input type=""text"" style=""width:auto;"" id=""yy"" name=""yy"" value=""").Append(Year(DateTime.Now)).Append(""" size=""4"" maxlength=""4"" tabindex=""4"" autocomplete=""off"" /> @ <input style=""width:auto;"" type=""text"" id=""hh"" name=""hh"" value=""").Append(Hour(DateTime.Now)).Append(""" size=""2"" maxlength=""2"" tabindex=""4"" autocomplete=""off"" /> : <input style=""width:auto;"" type=""text"" id=""mn"" name=""mn"" value=""").Append(Minute(DateTime.Now)).Append(""" size=""2"" maxlength=""2"" tabindex=""4"" autocomplete=""off"" /><input type=""hidden"" id=""ss"" name=""ss"" value=""").Append(Second(DateTime.Now)).Append(""" />")
        strOut.Append("<input type=""button"" value=""OK"" class=""button"" onclick=""javascript:Util.PublishConfirmAction();"" /> ")
        strOut.Append("<a onclick=""javascript:Util.CancelArticlePublish();"" title=""Cancel"" href=""#"">Cancel</a><input type=""hidden"" id=""txtReleaseDate"" name=""txtReleaseDate"" />")
        strOut.Append("<script type=""text/javascript"" language=""javascript"">gmobj(""mmmm"").value = ").Append(Month(DateTime.Now)).Append(";</script>")
        Return strOut.ToString()
    End Function

    Public Function WriteVisibilityInf()
        Dim strOut As New StringBuilder()
        strOut.Append("<br /><label><input type=""radio"" name=""cboVisible"" id=""txtArticlePublic"" value=""0"" checked=""checked"" onclick=""javascript:Util.ArticlePublic();"" />Public</label><br />")
        strOut.Append("<label><input type=""radio"" name=""cboVisible"" id=""txtArticlePassword"" value=""1"" onclick=""javascript:Util.ArticlePassword();"" />Password protected</label><br />")
        strOut.Append("<span id=""frmArticlePwd"" style=""font-size:11px;display:none;""><label>Password<br /><input type=""text"" id=""txtArticlePwd"" name=""txtArticlePwd"" /></label><br /></span>")
        strOut.Append("<label><input type=""radio"" name=""cboVisible"" id=""txtArticlePrivate"" value=""2"" onclick=""javascript:Util.ArticlePrivate();"" />Private</label><br />")
        strOut.Append("<input type=""button"" class=""button"" value=""OK"" onclick=""javascript:Util.ConfirmAction('").Append(ArticleVisibleContent.ClientID).Append("');"" />&nbsp;&nbsp;<a href=""#"" title=""Cancel"" onclick=""javascript:Util.CancelArticleVisible('").Append(ArticleVisibleContent.ClientID).Append("');"">Cancel</a>")
        strOut.Append("<input type=""hidden"" name=""txtPostVisible"" id=""txtPostVisible"" value=""0"" />")
        Return strOut.ToString()
    End Function

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If txtTitle.Text = vbNullString Then
            txtTitle.Text = "No Title"
        ElseIf txtTitle.Text.Length > 550 Then
            txtTitle.Text = Left(txtTitle.Text, 500)
        End If
        a.Status = False
        ArticleInfo()
        a.Create()
        a.GetAfterIns()
        a.WriteXMLByID(a.PageID, a.CreatedDate.Day, a.CreatedDate.Month, a.CreatedDate.Year, a.Body)

        Response.Redirect("/mt-inside/PageUpdate.aspx?ID=" & a.PageID & "&MsgID=2")
    End Sub

    Private Sub btnCreate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreate.Click
        If txtTitle.Text = vbNullString Then
            txtTitle.Text = "No Title"
        ElseIf txtTitle.Text.Length > 550 Then
            txtTitle.Text = Left(txtTitle.Text, 500)
        End If
        a.Status = True
        ArticleInfo()
        a.Create()
        a.PageID = PageID
        a.GetAfterIns()
        a.WriteXMLByID(a.PageID, a.CreatedDate.Day, a.CreatedDate.Month, a.CreatedDate.Year, a.Body)
        a.WriteTop10Last()
        Response.Redirect("/mt-inside/PageUpdate.aspx?ID=" & a.PageID & "&MsgID=6")
    End Sub

    Private Sub ArticleInfo()
        Dim rnd As New Utilities.RandomStrings()
        PageID = rnd.GenerateRandomString(DateTime.Now)
        a.PageID = PageID
        a.PageTitle = util2.NoHTML(txtTitle.Text)
        a.PageTitleNone = util2.utf8_to_ascii(a.PageTitle)
        a.Body = FCKeditor1.Value
        a.CommentsEnabled = drpCommentEnabled.Checked
        a.PostVisible = Integer.Parse(Request.Form("txtPostVisible").ToString())
        a.CreatedDate = DateTime.Now

        If util2.GetCookie("UserID") = vbNullString Then
            a.PostBy = Integer.Parse(util2.GetSession("UserID"))
        Else
            a.PostBy = Integer.Parse(util2.GetCookie("UserID"))
        End If

        If Request.Form.Get("txtArticlePwd") = vbNullString Then
            a.PagePassword = vbNullString
        Else
            a.PagePassword = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Request.Form("txtArticlePwd").ToString(), "MD5")
        End If

        If Request.Form("txtReleaseDate").ToString() = vbNullString Then
            a.ReleaseDate = DateTime.Now
        Else
            a.ReleaseDate = DateTime.Parse(Request.Form("txtReleaseDate").ToString())
        End If

        a.PagePath = "/Pages/" & a.PageID & "/" & a.PageTitleNone & ".htm"

        rnd = Nothing
        util2 = Nothing
    End Sub
End Class